package cn.pfund.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Table;

/**
 * @description 权限
 * @author zhaominglei qq:351024274，专注于网站、app和游戏开发。
 * @date 2014年9月20日
 */
@Entity
@Table(name="pfund_permission")
public class Permission extends OrderEntity implements Serializable {
	private static final long serialVersionUID = -4413711222440985063L;
	/** 树路径分隔符 */
	private static final String TREE_PATH_SEPARATOR = ",";
	/** 名称 */
	private String name;
	/** 全称 */
	private String fullName;
	/** 描述 */
    private String description;
    /** 权限 */
    private String permission;
    /** 树路径 */
	private String treePath;
    /** 上级权限 */
    private Permission parent; 
    /** 下级权限 */
    private Set<Permission> children = new HashSet<Permission>(); 
    /** 角色*/
    private Set<Role> roles = new HashSet<Role>(); 

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name="full_name", columnDefinition="LONGTEXT")
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	@Column(name="tree_path")
	public String getTreePath() {
		return treePath;
	}

	public void setTreePath(String treePath) {
		this.treePath = treePath;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="parent_id")
	public Permission getParent() {
		return parent;
	}

	public void setParent(Permission parent) {
		this.parent = parent;
	}

	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="pfund_role_permission", joinColumns = { @JoinColumn(name = "permission_id", updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "role_id", updatable = false) })
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade={CascadeType.ALL})
	public Set<Permission> getChildren() {
		return children;
	}

	public void setChildren(Set<Permission> children) {
		this.children = children;
	}
	
	/**
	 * 持久化前处理
	 */
	@PrePersist
	public void prePersist() {
		Permission parent = getParent();
		if (parent != null) {
			setFullName(parent.getFullName() + getName());
			setTreePath(parent.getTreePath() + parent.getId() + TREE_PATH_SEPARATOR);
		} else {
			setFullName(getName());
			setTreePath(TREE_PATH_SEPARATOR);
		}
	}

	/**
	 * 更新前处理
	 */
	@PreUpdate
	public void preUpdate() {
		Permission parent = getParent();
		if (parent != null) {
			setFullName(parent.getFullName() + getName());
		} else {
			setFullName(getName());
		}
	}

	/**
	 * 删除前处理
	 */
	@PreRemove
	public void preRemove() {
	}
	
	/**
	 * 重写toString方法
	 * 
	 * @return 全称
	 */
	@Override
	public String toString() {
		return getFullName();
	}
}
